<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>1-3 循环事件绑定：基于事件委托实现</title>
</head>

<body>
  <button index="0">第1个按钮</button>
  <button index="1">第2个按钮</button>
  <button index="2">第3个按钮</button>

  <script>
    document.body.addEventListener("click", function (e) {
      // console.log(e);
      let target = e.target,
        targetTag = target.tagName;
      // 判断是否为点击对象
      if (targetTag === "BUTTON") {
        var index = target.getAttribute("index");
        console.log(index);
      }
    })
  </script>
</body>

</html>